<?php

$uriReq = $_SERVER['REQUEST_URI'];
$reqMethod = strtolower($_SERVER['REQUEST_METHOD']);
$uriArray = explode("/", $uriReq);

switch($reqMethod){
    case 'get': getMeth($uriArray); break;
    case 'post': postMeth(); break;
    case 'put': putMeth(); break;
    case 'delete': delMeth(); break;
    default: break;
}

function getMeth($uriArray){
    if(isset($uriArray[2])){
        if($uriArray[2]=="all"){
            getAll();
        }
        else if($uriArray[2]=="international"){
            if(isset($uriArray[3])){
                if($uriArray[3]=="arrival"){
                    getSome(1, "from");
                }
                else if($uriArray[3]=="departure"){
                    getSome(2, "goingto");
                }else{displayDoc();}
            }else{displayDoc();}
        }
        else if($uriArray[2]=="domestic"){
            if(isset($uriArray[3])){
                if($uriArray[3]=="arrival"){
                    getSome(3, "from");
                }
                else if($uriArray[3]=="departure"){
                    getSome(4, "goingto");
                }else{displayDoc();}
            }else{displayDoc();}
        }
        else{displayDoc();}
    }else{
        displayDoc();
    }
}

function postMeth(){}
function putMeth(){}
function delMeth(){}

function getAll(){
    include 'simple_html_dom.php';
    //get all data from angkasapura
    $counter=1;
    header("content-type: text/xml");
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    echo '<allflight>';
    for($h=1; $h<5; $h++){
        for($i=1; $i<15; $i++){
        $url="http://www.angkasapura2.co.id/go.php?app=FlightInformation&page=".$i."&id=".$h;
        $html=file_get_html($url);
        $table=$html->find('table', 1);
        $skip=0;
            foreach ($table->find('tr') as $tr){
                if($skip!=0){
                    echo '<flight>';
                    echo '<num>'.$counter++.'</num>';
                    //echo '<airlines>'.$tr->find('td', 0).'</airlines>';
					if($i % 2==1)$t="from";
					else $t="goingto";
                    echo '<'.$t.'>'.$tr->find('td', 1)->plaintext.'</'.$t.'>';
                    echo '<flightnumber>'.$tr->find('td', 2)->plaintext.'</flightnumber>';
                    echo '<schedule>'.$tr->find('td', 3)->plaintext.'</schedule>';
                    echo '<actual>'.$tr->find('td', 4)->plaintext.'</actual>';
                    echo '<remark>'.$tr->find('td', 5)->plaintext.'</remark>';
                    echo '<terminal>'.$tr->find('td', 6)->plaintext.'</terminal>';
                    echo '<gate>'.$tr->find('td', 7)->plaintext.'</gate>';
                    echo '</flight>';
                }
                $skip++;
            }
        }
    }
        echo '</allflight>';
    //echo $table;
}
function getSome($id, $type){
    include 'simple_html_dom.php';
    //get all data from angkasapura
    $counter=1;
    header("content-type: text/xml");
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    echo '<allflight>';
    for($i=1; $i<15; $i++){
    $url="http://www.angkasapura2.co.id/go.php?app=FlightInformation&page=".$i."&id=".$id;
    $html=file_get_html($url);
    $table=$html->find('table', 1);
    $skip=0;
    foreach ($table->find('tr') as $tr){
        if($skip!=0){
            echo '<flight>';
            echo '<num>'.$counter++.'</num>';
            //echo '<airlines>'.$tr->find('td', 0).'</airlines>';
            echo '<'.$type.'>'.$tr->find('td', 1)->plaintext.'</'.$type.'>';
            echo '<flightnumber>'.$tr->find('td', 2)->plaintext.'</flightnumber>';
            echo '<schedule>'.$tr->find('td', 3)->plaintext.'</schedule>';
            echo '<actual>'.$tr->find('td', 4)->plaintext.'</actual>';
            echo '<remark>'.$tr->find('td', 5)->plaintext.'</remark>';
            echo '<terminal>'.$tr->find('td', 6)->plaintext.'</terminal>';
            echo '<gate>'.$tr->find('td', 7)->plaintext.'</gate>';
            echo '</flight>';
        }
        $skip++;
    }
    }
    echo '</allflight>';
}
//display the documentation page
function displayDoc(){
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
	<style>
		body{
		color: midnightblue;
		font-family: consolas, arial, times new roman;
		background: url("http://whatissap.co.in/wp-content/uploads/2012/05/Blue-Sky.jpg");
		}
		#cont{
		background-color: rgba(255, 255, 255, 0.5);
		border: royalblue solid 5px;
		border-radius: 15px;
		padding: 15px;
		}
		h1, h2{
		color: darkblue; 
		}
		span{
		border-radius: 5px;
		color: white;
		margin: 3px;
		}
		li{
		margin: 3px;
		}
		.api{
		background-color: midnightblue;
		}
		.api1{
		background-color: deepskyblue;
		}
		.api2{
		background-color: lightskyblue;
		}
	</style>
    <body>
	<div id="cont">
        <h1>Selamat datang di AngkasaWS</h1>
        <h2>API</h2>
        untuk mendapatkan semua jadwal penerbangan dari angkasapura2.co.id dalam bentuk xml: 
        <h2>Semua Penerbangan</h2>
		<ul>
        <li><span class="api">http://flo.ap01.aws.af.cm/angkasaWS</span><span class="api1">/all</span></li>
		</ul>
        <h2>Internasional</h2>
        <ul>
            <li>Departure: <span class="api">http://flo.ap01.aws.af.cm/angkasaWS</span><span class="api1">/international</span><span class="api2">/departure</span></li>
            <li>Arrival: <span class="api">http://flo.ap01.aws.af.cm/angkasaWS</span><span class="api1">/international</span><span class="api2">/arrival</span></span></li>
        </ul>
        <h2>Domestik</h2>
        <ul>
            <li>Departure: <span class="api">http://flo.ap01.aws.af.cm/angkasaWS</span><span class="api1">/domestic</span><span class="api2">/departure</span></li>
            <li>Arrival: <span class="api">http://flo.ap01.aws.af.cm/angkasaWS/</span><span class="api1">domestic</span><span class="api2">/arrival</span></li>
        </ul>
    </div>
	</body>
</html>
<?php
}
?>